package com.example.windowdemo.threadpool;

import android.os.Handler;
import android.os.Looper;

import com.example.windowdemo.callback.WindowRunnable;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author: njb
 * @date: 2023/8/3 11:34
 * @desc:
 */
public class WindowThreadPool {
    private static final int THREAD_POOL_SIZE = 4;
    private ExecutorService executorService;

    public WindowThreadPool() {
        executorService = Executors.newFixedThreadPool(THREAD_POOL_SIZE);
    }

    public void execute(WindowRunnable windowRunnable, WindowPool<?> window) {
        Looper.prepare();
        new Handler(Looper.getMainLooper()).post(new Runnable() {
            @Override
            public void run() {
                executorService.execute(() -> windowRunnable.run(window));
            }
        });
        Looper.loop();
    }

    public void shutdown() {
        executorService.shutdown();
    }
}
